<?php

require_once "core/application/models/mchange/mchange.php";
require_once "core/application/classes/errorClass.php";

class changePassword_Controller extends Change_Model {

    private static $_instance = NULL;
    public $_lang             = NULL;
    private $_params       = array();

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    private function catchParams() {
        $params = array(
            'oldPassword' => $_POST['old_password'],
            'newPassword' => $_POST['new_password'],
            'confirmPassword' => $_POST['new_password_confirm'],
        );
        return $params;
    }

    private function emptyPasswords($oldPassword, $newPassword, $confirmPassword) {
        if (!empty($oldPassword) && !empty($newPassword) && !empty($confirmPassword)) {
            return true;
        } else {
            return false;
        }
    }

    private function answers() {
        if ($this->getAnswer($_SESSION['name']) == $_POST['answer']) {
            return true;
        } else {
            return false;
        }
    }

    private function emails() {
        if ($this->getEmail($_SESSION['name']) == $_POST['mail']) {
            return true;
        } else {
            return false;
        }
    }

    private function passwords($newPassword, $newPasswordConfirm) {
        if ($newPassword == $newPasswordConfirm) {
            return true;
        } else {
            return false;
        }
    }

    public function change_password() {
        $this->_lang = $this->getLanguage();
        if (!$this->answers()) {
            $this->echoError("Не верный ответ на вопрос <br />");
        }
        if (!$this->emails()) {
            $this->echoError("Введенный Вами адрес электронной почты не верен <br />");
        }
        if (!$this->passwords($_POST['new_password'], $_POST['new_password_confirm'])) {
            $this->redirect(10, "index?settings&change_password");
            $this->echoError("Новые пароли не совпадают");
        }
        $this->_params = $this->catchParams();
        try {
            foreach ($this->_params as $key => $val) {
                if (empty($val) || !isset($val) || is_null($val))
                    $this->echoError("Не заполнен один из пунктов");
                $this->redirect(10, "index?change_password");
                unset($key);
            }
        } catch (Exception $exc) {
            echo $exc->getMessage();
        }

        $this->_params['oldPassword'] = $this->secure($this->_params['oldPassword']);
        $this->_params['newPassword'] = $this->secure($this->_params['newPassword']);
        $this->_params['confirmPassword'] = $this->secure($this->_params['confirmPassword']);
        if (!$this->emptyPasswords($this->_params['oldPassword'], $this->_params['newPassword'], $this->_params['confirmPassword'])) {
            $this->echoError("Не все поля с паролями заполнены");
        }
        if (strlen($this->_params['newPassword']) < 5 || strlen($this->_params['newPassword']) > 45) {
            $this->redirect(10, "index?settings&change_password");
            $this->echoError("Пароль не может быть короче 5 символов и длинее 45");
        }
        $this->_params['newPassword'] = $this->cryptPassword($this->_params['newPassword']);
        $this->_params['oldPassword'] = $this->cryptPassword($this->_params['oldPassword']);
        
        try {
            $this->updatePassword($this->_params['newPassword'], $_SESSION['name'], $this->_params['oldPassword']);
            $this->echoSuccess("Пароль успешно изменен");
            unset($this->_params, $this->_lang);
        } catch (Exception $e) {
            echo $e->getMessage();
            echo $e->getLine();
            unset($this->_params, $this->_lang);
            error::getInstance($this->_lang->language['defaultError']);
        }
    }

}
?>
